package saucaca.fragment;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;

import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class InfoDetailFragment extends Fragment {
	String guideID;
	TextView txtContent;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	String empty = "No info!";
	WebView webView;
	
	public static InfoDetailFragment newInstance(String SongId) {
		InfoDetailFragment f = new InfoDetailFragment();
		Bundle args = new Bundle();
		args.putString(MainActivity.ID, SongId);
		f.setArguments(args);
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.about, container, false);
		findviewByID(android);
		if (getArguments() != null) {
			Bundle b = getArguments();
			guideID = b.getString(MainActivity.ID);
		}
		if (guideID != null) {
			new getGuide().execute("");
		} else {
			progressBar.setVisibility(View.GONE);
			txtStatus.setText(empty);
			imgEmpty.setVisibility(View.VISIBLE);
		}
		return android;
	}

	void findviewByID(View view) {
		txtContent = (TextView) view.findViewById(R.id.about_textInfo);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
		webView=(WebView) view.findViewById(R.id.new_detail_webView);
		final WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setSupportMultipleWindows(true);
		webSettings.setTextSize(WebSettings.TextSize.NORMAL);
	}

	class getGuide extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.guide(guideID);
			
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
				try {
					String txurl=JsonReader.guideURL(result);
					BaseEngine.showLogformLongString(txurl);
					new getGuide1().execute(txurl);
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}

	}
	
	public String getJson(String url) {
		Log.e("getJson", "url: " + url);
		InputStream is = null;
		String result = "";
		try {
//			HttpClient httpclient = new DefaultHttpClient();
//			HttpPost httppost = new HttpPost(url);
//			HttpResponse response = httpclient.execute(httppost);
//			HttpEntity entity = response.getEntity();
//			is = entity.getContent();
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			is = httpEntity.getContent();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result = sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return result;

	}
	class getGuide1 extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			String str =getJson(params[0]);
			
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
				try {
					BaseEngine.showLogformLongString(result);
//					String txurl=JsonReader.guideURL(result);
					final String mimeType = "text/html";
					final String encoding = "utf-8";
					String data = Html.fromHtml(result).toString();
					data = data.replace("<img", "<img style=\"width:100%!important\"");
					webView.loadDataWithBaseURL(null, data, mimeType, encoding, null);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}

	}

}
